# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import os
import requests
import pymongo


class DoutuPipeline_bd(object):
    def __init__(self):
        self._id = 1

    def process_item(self, item, spider):
        src = item['src']
        alt = item['alt']
        tu_name = src[-4:]
        response = requests.get(url=src)
        os.makedirs('最新表情', exist_ok=True)
        with open('最新表情/' + str(self._id) + alt + tu_name, 'wb+') as f:
            f.write(response.content)
        src_bd = os.getcwd() + '最新表情/' + str(self._id) + alt + tu_name
        item['_id'] = self._id
        item['src_bd'] = src_bd
        self._id += 1
        return item


class DoutuPipeline(object):
    def __init__(self):
        self.myclient = pymongo.MongoClient('127.0.0.1', 27017)
        if '斗图' in self.myclient.list_database_names():
            self.myclient.drop_database('斗图')
        self.mydb = self.myclient['斗图']
        self.mycol = self.mydb['最新']

    def process_item(self, item, spider):
        self.mycol.insert_one(dict(item))
        return item
